#include "QuoteLvKIndex.h"


double QuoteLvKIndex_RSV(QUOTELV_K_INDEX* aKIndex, QUOTELV_K_POINT* aPoint, int aKCount, int aStartPos);

/**计算周期内的最小值
*
*  @param QUOTELV_K_INDEX	*aKIndex		K线操作结构指针
*  @param aKCount			aKCount			K线的数量
*  @param int				aStartPos		起始位置
*  @param char			aCycle			周期
*
*  @return -
*
*		
*/
double QuoteLvKIndex_LLV(QUOTELV_K_POINT* aPoint,int aKCount, int aStartPos, int aCycle);

/**计算周期内的最大值
*
*  @param QUOTELV_K_INDEX	*aKIndex		K线操作结构指针
*  @param aKCount			aKCount			K线的数量
*  @param int				aStartPos		起始位置
*  @param char			aCycle			周期
*
*  @return -
*
*		
*/
double QuoteLvKIndex_HHV(QUOTELV_K_POINT* aPoint,int aKCount, int aStartPos, int aCycle);

/**计算SMA值
*
*  @param QUOTELV_K_INDEX	*aKIndex		K线操作结构指针
*  @param int				aCycle			周期（K线数量）
*  @param int				aM				权重（K线数量）
*
*  @return -
*
*		
*/
double QuoteLvKIndex_calcSMA(double aPreEMA, double x, int aCycle, double aM);

/**计算SMA值
*
*  @param QUOTELV_K_INDEX	*aKIndex		K线操作结构指针
*  @param int				aCycle			周期（K线数量）
*  @param int				aM				权重（K线数量）
*
*  @return -
*
*		
*/
double QuoteLvKIndex_calcEXPMEMA(double aPoint[], int aCount, double x, int aCycle);

/**计算估算标准差
*
*  @param double			aPoint			数组操作指针
*  @param int				aCycle			周期（K线数量）
*  @param int				aM				权重（K线数量）
*  @param double			avg				平均值
*
*  @return -
* 注释:
*		
*/
double QuoteLvKIndex_calcEMA(double aPreEMA, double x, int aCycle);

/**计算报价MA值
*
*  @param QUOTELV_K_INDEX	*aKIndex		K线操作结构指针
*  @param int				aStartPos		起始位置
*  @param int				aCycle			周期（K线数量）
*  @param int				aM				权重（K线数量）
*
*  @return -
*
*		
*/
double QuoteLvKIndex_calcMA(QUOTELV_K_POINT* aPoint, int aStartPos, int aCycle);

/**计算成交量MA值
*
*  @param QUOTELV_K_INDEX	*aKIndex		K线操作结构指针
*  @param int				aStartPos		起始位置
*  @param int				aCycle			周期（K线数量）
*  @param int				aM				权重（K线数量）
*
*  @return -
*
*		
*/
double QuoteLvKIndex_calcVMA(QUOTELV_K_POINT* aPoint, int aStartPos, int aCycle);

/**计算MA值
*
*  @param double			*aPoint			数组操作指针
*  @param int				aStartPos		起始位置
*  @param int				aCycle			周期（K线数量）
*  @param int				aM				权重（K线数量）
*
*  @return -
*
*		
*/
double QuoteLvKIndex_calcArrayMA(double aPoint[], int aStartPos, int aCycle);

/**计算估算标准差
*
*  @param double			aPoint			数组操作指针
*  @param int				aStartPos		起始位置
*  @param int				aCycle			周期（K线数量）
*  @param int				aM				权重（K线数量）
*  @param double			avg				平均值
*
*  @return -
*
*		
*/
double QuoteLvKIndex_calcSTD(QUOTELV_K_POINT* aPoint, int aStartPos, int aCycle, double aAvg);

/**计算估算标准差
 *
 *  @param double			aPoint			数组操作指针
 *  @param int				aStartPos		起始位置
 *  @param int				aCycle			周期（K线数量）
 *  @param int				aM				权重（K线数量）
 *  @param double			avg				平均值
 *
 *  @return -
 *
 *		
 */
double QuoteLvKIndex_calcArraySTD(double aPoint[], int aStartPos, int aCycle, double aAvg);

/**计算平均绝对偏差
 *
 *  @param double			aPoint			数组操作指针
 *  @param int				aStartPos		起始位置
 *  @param int				aCycle			周期（K线数量）
 *  @param int				aM				权重（K线数量）
 *  @param double			avg				平均值
 *
 *  @return -
 *
 *		
 */
double QuoteLvKIndex_calcAVEDEV(double aPoint[], int aStartPos, int aCycle, double aAvg);

/**计算周期内总和
 *
 *  @param double			aPoint			数组操作指针
 *  @param int				aStartPos		起始位置
 *  @param int				aCycle			周期（K线数量）
 *
 *  @return -
 *
 *		
 */
double QuoteLvKIndex_calcSUM(double aPoint[], int aStartPos, int aCycle);

/**计算long64 周期内总和
 *
 *  @param long64			aPoint			数组操作指针
 *  @param int				aStartPos		起始位置
 *  @param int				aCycle			周期（K线数量）
 *
 *  @return -
 *
 *		
 */
long64 QuoteLvKIndex_calcLong64SUM(long64 aPoint[], int aStartPos, int aCycle);

/**计算满足条件的周期数
 *
 *  @param double			aPoint			数组操作指针
 *  @param int				aStartPos		起始位置
 *  @param int				aCycle			周期（K线数量）
 *
 *  @return -
 *
 *		
 */
double QuoteLvKIndex_calcCOUNT(QUOTELV_K_POINT* aPoint, int aStartPos, int aCycle);




QUOTELV_K_INDEX* QuoteLvKIndex_New()
{
  long64 length = sizeof(QUOTELV_K_INDEX);
	QUOTELV_K_INDEX *KIndex = (QUOTELV_K_INDEX *) Tools_Malloc(sizeof(QUOTELV_K_INDEX));

	//初始化KDJ参数
	KIndex->KDJ_N = 9;
	KIndex->KDJ_M1 = 3;
	KIndex->KDJ_M2 = 3;

	//初始化DMA参数
	KIndex->DMA_SHORT = 10;
	KIndex->DMA_LONG = 50;
	KIndex->DMA_M = 10;

	//初始化BOLL参数
	KIndex->BOLL_N = 26;
	KIndex->BOLL_P = 2;
	
	//初始化BOLL_MAIN参数
	KIndex->BOLL_MAIN_N = 20;
	KIndex->BOLL_MAIN_P = 2;
	
	//初始化RSI参数
	KIndex->RSI_N1 = 6;
	KIndex->RSI_N2 = 12;
	KIndex->RSI_N3 = 24;

	//初始化MACD参数
	KIndex->MACD_SHORT = 12;
	KIndex->MACD_LONG = 26;
	KIndex->MACD_MID = 9;

	//初始化价格均线参数
	KIndex->PAVG_N1 = 5;
	KIndex->PAVG_N2 = 10;
	KIndex->PAVG_N3 = 20;
	KIndex->PAVG_N4 = 60;

	//初始化DMI参数
	KIndex->DMI_N = 14;
	KIndex->DMI_MM = 6;

	//初始化成交量均线参数
	KIndex->VAVG_N1 = 5;
	KIndex->VAVG_N2 = 10;
	
	//初始化威廉指标参数
	KIndex->WR_N = 14;
	KIndex->WR_N1 = 28;
	
	//初始化商品路径指标参数
	KIndex->CCI_N = 14;
	
	//初始化BIAS指标参数
	KIndex->BIAS_L1 = 6;
	KIndex->BIAS_L2 = 12;
	KIndex->BIAS_L3 = 24;
	
	//初始化ARBR指标参数
	KIndex->ARBR_N = 26;

	//初始化PSY指标参数
	KIndex->PSY_N = 12;
  
  //初始化累积能量线指标参数
  KIndex->OBV_N = 10;//默认30根，暂用10根
	
	memset(KIndex->DDX.ddx,0,sizeof(KIndex->DDX.ddx));
	memset(KIndex->DDX.ddx1,0,sizeof(KIndex->DDX.ddx1));
	memset(KIndex->DDX.ddx2,0,sizeof(KIndex->DDX.ddx2));
	memset(KIndex->DDX.ddx3,0,sizeof(KIndex->DDX.ddx3));
	
	memset(KIndex->BBDRetai.shareholdingRatio,0,sizeof(KIndex->BBDRetai.shareholdingRatio));
	memset(KIndex->BBDRetai.currentFluctuation,0,sizeof(KIndex->BBDRetai.currentFluctuation));
	
	memset(KIndex->BBDMain.shareholdingRatio,0,sizeof(KIndex->BBDMain.shareholdingRatio));
	memset(KIndex->BBDMain.currentFluctuation,0,sizeof(KIndex->BBDMain.currentFluctuation));
	
	memset(KIndex->BBD.bbd,0,sizeof(KIndex->BBD.bbd));
	memset(KIndex->BBD.sum1,0,sizeof(KIndex->BBD.sum1));
	memset(KIndex->BBD.sum2,0,sizeof(KIndex->BBD.sum2));
	memset(KIndex->BBD.sum3,0,sizeof(KIndex->BBD.sum3));
	
	memset(KIndex->AVG_VOL_SUM.avgVol,0,sizeof(KIndex->AVG_VOL_SUM.avgVol));
	memset(KIndex->AVG_VOL_SUM.avgSum,0,sizeof(KIndex->AVG_VOL_SUM.avgSum));
	
	return KIndex;
}

void QuoteLvKIndex_calcKDJ(QUOTELV_K_INDEX* aKIndex, QUOTELV_K_RES* aRes)
{
	int i = 0;
	double RSVt[KLIN_MAX_NUM];
	//int sPos = aRes->nMaxCount - aRes->Added[1][1] - aRes->nTotalCount;		//起始位置
	int count = aRes->nMaxCount;					//计算的个数（总数-周期数-起始位置）

	for(i=0; i<count; i++)
	{
		RSVt[i] = QuoteLvKIndex_RSV(aKIndex,aRes->kData,aRes->nMaxCount,i);
	}

	aKIndex->KDJ.K[0] = RSVt[0];
	aKIndex->KDJ.D[0] = RSVt[0];

	for(i=1; i<count; i++)
	{
		aKIndex->KDJ.K[i] = QuoteLvKIndex_calcSMA(aKIndex->KDJ.K[i-1],RSVt[i],aKIndex->KDJ_M1,1);

	}
	
	

	for (i=1; i<count; i++)
	{
		aKIndex->KDJ.D[i] = QuoteLvKIndex_calcSMA(aKIndex->KDJ.D[i-1], aKIndex->KDJ.K[i], aKIndex->KDJ_M2,1);
		//aKIndex->KDJ.J[i] = 3*aKIndex->KDJ.K[i] - 2*aKIndex->KDJ.D[i];
	}
	
	for (i = 0; i < count; i++) {
		aKIndex->KDJ.J[i] = 3*aKIndex->KDJ.K[i] - 2*aKIndex->KDJ.D[i];
	}
	
	aKIndex->KDJ.J[0] = aKIndex->KDJ.D[0];

	for (i= aRes->nMaxCount-1; i>=aRes->nKStartPos; i--)
	{
		aKIndex->KDJ.K[i] = aKIndex->KDJ.K[count-1];
		aKIndex->KDJ.D[i] = aKIndex->KDJ.D[count-1];
		aKIndex->KDJ.J[i] = aKIndex->KDJ.J[count-1];
		count--;
	}
	
	
}

void QuoteLvKIndex_calcDMA(QUOTELV_K_INDEX* aKIndex, QUOTELV_K_RES* aRes)
{
	/*
	 DDD : (MA(CLOSE,SHORT)-MA(CLOSE,LONG));
	 AMA : MA(DDD,M);
	 */
	int MaxCycle = aKIndex->DMA_SHORT > aKIndex->DMA_LONG ? aKIndex->DMA_SHORT : aKIndex->DMA_LONG;
	int sPos = aRes->nMaxCount - aRes->nTotalCount - MaxCycle;		//起始位置
	sPos = sPos > 0 ? sPos : 0;
	
	int i = 0;
	double MA1[KLIN_MAX_NUM];
	double MA2[KLIN_MAX_NUM];
	
	for (i = sPos; i<aRes->nMaxCount; i++) {
		MA1[i] = QuoteLvKIndex_calcMA(aRes->kData, i, aKIndex->DMA_SHORT);
		MA2[i] = QuoteLvKIndex_calcMA(aRes->kData, i, aKIndex->DMA_LONG);
	}
	
	sPos = aRes->nMaxCount - aRes->nTotalCount;
	
	for (i = sPos; i<aRes->nMaxCount; i++) {
		aKIndex->DMA.DDD[i] = MA1[i] - MA2[i];
	}
	
	for (i = sPos; i<aRes->nMaxCount; i++) {
		aKIndex->DMA.AMA[i] = QuoteLvKIndex_calcArrayMA(aKIndex->DMA.DDD,i,aKIndex->DMA_M);
	}
	
}

void QuoteLvKIndex_calcBOLL(QUOTELV_K_INDEX* aKIndex, QUOTELV_K_RES* aRes)
{
	/*
	MID :  MA(CLOSE,N);
	UPPER: MID + P*STD(CLOSE,N);
	LOWER: MID - P*STD(CLOSE,N);
	*/
	
	int sPos = aRes->nMaxCount - aRes->nTotalCount - aKIndex->BOLL_N;		//起始位置
	sPos = sPos > 0 ? sPos : 0;
	
	int i = 0;
	double MA[KLIN_MAX_NUM];
	double STD[KLIN_MAX_NUM];
	
	for (i = sPos; i<aRes->nMaxCount; i++) {
		MA[i] = QuoteLvKIndex_calcMA(aRes->kData, i, aKIndex->BOLL_N);
		STD[i] = QuoteLvKIndex_calcSTD(aRes->kData, i, aKIndex->BOLL_N, MA[i]);
	}
	
	sPos = aRes->nMaxCount - aRes->nTotalCount;
	
	for (i = sPos; i<aRes->nMaxCount; i++) {
		aKIndex->BOLL.MID[i] = MA[i];
		aKIndex->BOLL.UPPER[i] = aKIndex->BOLL.MID[i] + aKIndex->BOLL_P * STD[i];
		aKIndex->BOLL.LOWER[i] = aKIndex->BOLL.MID[i] - aKIndex->BOLL_P * STD[i];
	}
}

void QuoteLvKIndex_calcMainBOLL(QUOTELV_K_INDEX* aKIndex, QUOTELV_K_RES* aRes)
{
	/*
	 N:=20;
	 MID:=MA(C,N);
	 VAR1:=POW((C-MID),2);
	 VAR2:=MA(VAR1,N);
	 VAR3:=SQRT(VAR2);
	 UPPER:=MID+2*VAR3;
	 LOWER:=MID-2*VAR3;
	 BOLL:REF(MID,1);
	 UB:REF(UPPER,1);
	 LB:REF(LOWER,1);
	 */
	
	 int sPos = aRes->nMaxCount - aRes->nTotalCount - aKIndex->BOLL_MAIN_N;		//起始位置
	 sPos = sPos > 0 ? sPos : 0;
	 
	 int i = 0;
	 double MA[KLIN_MAX_NUM];
	 QUOTELV_K_POINT  VAR1[KLIN_MAX_NUM];
	 double VAR2 = 0;
	 double VAR3 = 0;
	 
	 for (i = sPos; i<aRes->nMaxCount; i++) {
		 MA[i] = QuoteLvKIndex_calcMA(aRes->kData, i, aKIndex->BOLL_MAIN_N);
		 VAR1[i].fCur = pow(((double)(aRes->kData[i].fCur) - MA[i]),2.0);
		 
		 VAR2 = QuoteLvKIndex_calcMA(&VAR1, i, aKIndex->BOLL_MAIN_N);
		 VAR3 = sqrt(VAR2);
		 
		 aKIndex->BOLL_MAIN.MID[i] = MA[i];
		 aKIndex->BOLL_MAIN.UPPER[i] = aKIndex->BOLL_MAIN.MID[i] + aKIndex->BOLL_MAIN_P * VAR3;
		 aKIndex->BOLL_MAIN.LOWER[i] = aKIndex->BOLL_MAIN.MID[i] - aKIndex->BOLL_MAIN_P * VAR3; 
	 }
	
	 //sPos = aRes->nMaxCount - aRes->nTotalCount;
	
	 for (i = aRes->nMaxCount; i>0; i--) {
		 aKIndex->BOLL_MAIN.MID[i] = aKIndex->BOLL_MAIN.MID[i-1];
		 aKIndex->BOLL_MAIN.UPPER[i] = aKIndex->BOLL_MAIN.UPPER[i-1];
		 aKIndex->BOLL_MAIN.LOWER[i] = aKIndex->BOLL_MAIN.LOWER[i-1]; 
	 }
}

void QuoteLvKIndex_calcRSI(QUOTELV_K_INDEX* aKIndex, QUOTELV_K_RES* aRes)
{
	int i = 1;
	//int j = 1;
	//double rsi = 0;
	double maxSMA[KLIN_MAX_NUM];
	double absSMA[KLIN_MAX_NUM];

	maxSMA[0] = 0;//max(aRes->kData[0].fCur,0);///aKIndex->RSI_N1;
	absSMA[0] = 0;//fabs(aRes->kData[0].fCur);///aKIndex->RSI_N1;
	//rsi = maxSMA[0] /absSMA[0]*100;

	for(i=1; i<aRes->nMaxCount; i++)
	{
		maxSMA[i] = QuoteLvKIndex_calcSMA(maxSMA[i-1],max(aRes->kData[i].fCur - aRes->kData[i-1].fCur,0),aKIndex->RSI_N1,1);
		absSMA[i] = QuoteLvKIndex_calcSMA(absSMA[i-1],fabs(aRes->kData[i].fCur - aRes->kData[i-1].fCur),aKIndex->RSI_N1,1);
		//if (i==aKIndex->RSI_N1-2) {
//			aKIndex->RSI.RSI1[i] = maxSMA[i] / absSMA[i] * 50;
//		} else {
			aKIndex->RSI.RSI1[i] = maxSMA[i] / absSMA[i] * 100;
		//}
	}
	aKIndex->RSI.RSI1[0] = 0; 

	maxSMA[0] = 0;//max(aRes->kData[0].fCur,0)/aKIndex->RSI_N2;
	absSMA[0] = 0;//fabs(aRes->kData[0].fCur)/aKIndex->RSI_N2;
	//rsi = maxSMA[0] /absSMA[0]*100;

	for(i=1; i<aRes->nMaxCount; i++)
	{
		maxSMA[i] = QuoteLvKIndex_calcSMA(maxSMA[i-1],max(aRes->kData[i].fCur - aRes->kData[i-1].fCur,0),aKIndex->RSI_N2,1);
		absSMA[i] = QuoteLvKIndex_calcSMA(absSMA[i-1],fabs(aRes->kData[i].fCur - aRes->kData[i-1].fCur),aKIndex->RSI_N2,1);
		aKIndex->RSI.RSI2[i] = maxSMA[i] / absSMA[i] * 100;
	}
	aKIndex->RSI.RSI2[0] = 0 ;

	maxSMA[0] = 0;//max(aRes->kData[0].fCur,0)/aKIndex->RSI_N3;
	absSMA[0] = 0;//fabs(aRes->kData[0].fCur)/aKIndex->RSI_N3;
	//rsi = maxSMA[0] /absSMA[0]*100;

	for(i=1; i<aRes->nMaxCount; i++)
	{
		maxSMA[i] = QuoteLvKIndex_calcSMA(maxSMA[i-1],max(aRes->kData[i].fCur - aRes->kData[i-1].fCur,0),aKIndex->RSI_N3,1);
		absSMA[i] = QuoteLvKIndex_calcSMA(absSMA[i-1],fabs(aRes->kData[i].fCur - aRes->kData[i-1].fCur),aKIndex->RSI_N3,1);
		aKIndex->RSI.RSI3[i] = maxSMA[i] / absSMA[i] * 100;
	}
	aKIndex->RSI.RSI3[0] = 0;
}

void QuoteLvKIndex_calcMACD(QUOTELV_K_INDEX* aKIndex, QUOTELV_K_RES* aRes)
{
	aKIndex->MACD.DIF[0] = 0;
	aKIndex->MACD.DEA[0] = 0;
	aKIndex->MACD.MACD[0] = 0;


	aKIndex->MACD.shortEMA[0] = aRes->kData[0].fCur;
	aKIndex->MACD.longEMA[0] = aRes->kData[0].fCur;
	int count = aRes->nMaxCount;					//计算的个数（总数-周期数-起始位置）

	int i = 1;
	for(; i<count; i++)
	{
		aKIndex->MACD.shortEMA[i] = QuoteLvKIndex_calcEMA(aKIndex->MACD.shortEMA[i-1],aRes->kData[i].fCur,aKIndex->MACD_SHORT);
		aKIndex->MACD.longEMA[i] = QuoteLvKIndex_calcEMA(aKIndex->MACD.longEMA[i-1],aRes->kData[i].fCur,aKIndex->MACD_LONG);
		aKIndex->MACD.DIF[i] = aKIndex->MACD.shortEMA[i]- aKIndex->MACD.longEMA[i];
		aKIndex->MACD.DEA[i] = QuoteLvKIndex_calcEMA(aKIndex->MACD.DEA[i-1],aKIndex->MACD.DIF[i],aKIndex->MACD_MID);
		aKIndex->MACD.MACD[i] = (aKIndex->MACD.DIF[i] - aKIndex->MACD.DEA[i])*2;
	}
}

void QuoteLvKIndex_calcPriceAVG(QUOTELV_K_INDEX* aKIndex, QUOTELV_K_RES* aRes)
{
	int i = 0;

	for(i = aRes->nKStartPos; i<aRes->nMaxCount; i++)			
	{
		aKIndex->PAVG.M5[i] = QuoteLvKIndex_calcMA(aRes->kData,i,aKIndex->PAVG_N1);
	}


	for(i = aRes->nKStartPos; i<aRes->nMaxCount; i++)			
	{
		aKIndex->PAVG.M10[i] = QuoteLvKIndex_calcMA(aRes->kData,i,aKIndex->PAVG_N2);
	}
	


	for(i = aRes->nKStartPos; i<aRes->nMaxCount; i++)			
	{
		aKIndex->PAVG.M20[i] = QuoteLvKIndex_calcMA(aRes->kData,i,aKIndex->PAVG_N3);
	}

	for(i = aRes->nKStartPos; i<aRes->nMaxCount; i++)		
	{
		aKIndex->PAVG.M60[i] = QuoteLvKIndex_calcMA(aRes->kData,i,aKIndex->PAVG_N4);
	}

}

void QuoteLvKIndex_calcVolAVG(QUOTELV_K_INDEX* aKIndex, QUOTELV_K_RES* aRes)
{
	int i = 0;

	for(i=aRes->nKStartPos; i<aRes->nMaxCount; i++)				
	{
		aKIndex->VAVG.M5[i] = QuoteLvKIndex_calcVMA(aRes->kData,i,aKIndex->VAVG_N1);
	}

	for(i=aRes->nKStartPos; i<aRes->nMaxCount; i++)			
	{
		aKIndex->VAVG.M10[i] = QuoteLvKIndex_calcVMA(aRes->kData,i,aKIndex->VAVG_N2);
	}
}

void QuoteLvKIndex_calcDMI(QUOTELV_K_INDEX* aKIndex, QUOTELV_K_RES* aRes)
{
	int i = 1;
	int sPos = aRes->nMaxCount - aRes->nTotalCount - aRes->Added[1][1];
	int count = aRes->nMaxCount - sPos;
	double HD = aRes->kData[sPos].fHigh;
	double LD = aRes->kData[sPos].fLow;
	double DP = 0;
	double DM = 0;
	double MTR[KLIN_MAX_NUM];
	double DMP[KLIN_MAX_NUM];
	double DMM[KLIN_MAX_NUM];
	
	MTR[0] = (aRes->kData[sPos].fHigh - aRes->kData[sPos].fLow)/aKIndex->DMI_N;
	MTR[0] = MTR[0] != 0 ? MTR[0] : 0.01;
	DP = 0;

	if (HD > 0 && HD > LD)
	{
		DP = HD;
	}

	DMP[0] = DP/aKIndex->DMI_N;

	DM = 0;

	if (LD > 0 && LD > HD)
	{
		DM = LD;
	}

	DMM[0] = DM/aKIndex->DMI_N;

	aKIndex->DMI.PDI[0] = DMP[0]*100/MTR[0];
	aKIndex->DMI.MDI[0] = DMM[0]*100/MTR[0];

	aKIndex->DMI.ADX[0] = (fabs(aKIndex->DMI.MDI[0]-aKIndex->DMI.PDI[0])/(aKIndex->DMI.MDI[0]+aKIndex->DMI.PDI[0])*100)/aKIndex->DMI_MM;
	aKIndex->DMI.ADXR[0] = aKIndex->DMI.ADX[0]/aKIndex->DMI_MM;


	for (i=1; i<count; i++)
	{
		MTR[i] = QuoteLvKIndex_calcEXPMEMA(MTR,i,max(max(aRes->kData[i+sPos].fHigh-aRes->kData[i+sPos].fLow,
			fabs(aRes->kData[i+sPos].fHigh - aRes->kData[i+sPos-1].fCur)),fabs(aRes->kData[i+sPos-1].fCur - aRes->kData[i+sPos].fLow)),aKIndex->DMI_N);
		MTR[i] = MTR[i] != 0 ? MTR[i] : 0.01;
		HD = aRes->kData[i+sPos].fHigh - aRes->kData[i+sPos-1].fHigh;
		LD = aRes->kData[i+sPos-1].fLow - aRes->kData[i+sPos].fLow;

		DP = 0;

		if (HD > 0 && HD > LD)
		{
			DP = HD;
		}

		DM = 0;

		if (LD > 0 && LD > HD)
		{
			DM = LD;
		}

		DMP[i] = QuoteLvKIndex_calcEXPMEMA(DMP,i,DP,aKIndex->DMI_N);
		DMM[i] = QuoteLvKIndex_calcEXPMEMA(DMM,i,DM,aKIndex->DMI_N);

		aKIndex->DMI.PDI[i] = DMP[i]*100/MTR[i];
		aKIndex->DMI.MDI[i] = DMM[i]*100/MTR[i];

			/*MTR:=EXPMEMA(MAX(MAX(HIGH-LOW,ABS(HIGH-REF(CLOSE,1))),ABS(REF(CLOSE,1)-LOW)),N);
	HD :=HIGH-REF(HIGH,1);
	LD :=REF(LOW,1)-LOW;
	DMP:=EXPMEMA(IF(HD>0&&HD>LD,HD,0),N);
	DMM:=EXPMEMA(IF(LD>0&&LD>HD,LD,0),N);
	PDI: DMP*100/MTR;
	MDI: DMM*100/MTR;
	ADX: EXPMEMA(ABS(MDI-PDI)/(MDI+PDI)*100,MM);
	ADXR:EXPMEMA(ADX,MM);*/

		aKIndex->DMI.ADX[i] = QuoteLvKIndex_calcEXPMEMA(aKIndex->DMI.ADX,i,
			(fabs(aKIndex->DMI.MDI[i]-aKIndex->DMI.PDI[i])/(aKIndex->DMI.MDI[i]+aKIndex->DMI.PDI[i]))*100,aKIndex->DMI_MM);
		aKIndex->DMI.ADXR[i] = QuoteLvKIndex_calcEXPMEMA(aKIndex->DMI.ADXR,i,aKIndex->DMI.ADX[i],aKIndex->DMI_MM);


	}

}

void QuoteLvKIndex_calcWR(QUOTELV_K_INDEX* aKIndex, QUOTELV_K_RES* aRes)
{
	/*
	 WR1:100*(HHV(HIGH,N)-CLOSE)/(HHV(HIGH,N)-LLV(LOW,N));
	 WR2:100*(HHV(HIGH,N1)-CLOSE)/(HHV(HIGH,N1)-LLV(LOW,N1));
	*/
	
	int sPos = aRes->nMaxCount - aRes->nTotalCount - aKIndex->CCI_N;		//起始位置
	sPos = sPos > 0 ? sPos : 0;
	int i = sPos;
	double hhv = 0.0;
	double llv = 0.0;
	
	for (; i<aRes->nMaxCount; i++) {
		hhv = QuoteLvKIndex_HHV(aRes->kData,aRes->nMaxCount,i,aKIndex->WR_N);
		llv = QuoteLvKIndex_LLV(aRes->kData,aRes->nMaxCount,i,aKIndex->WR_N);
		aKIndex->WR.WR1[i] =  (100.0 * (hhv - aRes->kData[i].fCur))/(hhv - llv);
		hhv = QuoteLvKIndex_HHV(aRes->kData,aRes->nMaxCount,i,aKIndex->WR_N1);
		llv = QuoteLvKIndex_LLV(aRes->kData,aRes->nMaxCount,i,aKIndex->WR_N1);
		aKIndex->WR.WR2[i] =  (100.0 * (hhv - aRes->kData[i].fCur))/(hhv - llv);
	}
	
}

void QuoteLvKIndex_calcCCI(QUOTELV_K_INDEX* aKIndex, QUOTELV_K_RES* aRes)
{
	/*
	 TYP:=(HIGH+LOW+CLOSE)/3;
	 CCI:(TYP-MA(TYP,N))/(0.015*AVEDEV(TYP,N));
	*/
	int sPos = aRes->nMaxCount - aRes->nTotalCount - aKIndex->CCI_N;		//起始位置
	sPos = sPos > 0 ? sPos : 0;
	
	int i = 0;
	double TYP[KLIN_MAX_NUM];
	double TYPAVG[KLIN_MAX_NUM];
	
	for (i = sPos; i<aRes->nMaxCount; i++) {
		TYP[i] = (aRes->kData[i].fHigh + aRes->kData[i].fLow + aRes->kData[i].fCur)/3;
		TYP[i] = TYP[i] <= 0 ? aRes->kData[i].fCur : TYP[i];
	}
	
	for (i = sPos; i<aRes->nMaxCount; i++) {
		TYPAVG[i] = QuoteLvKIndex_calcArrayMA(TYP,i,aKIndex->CCI_N);
	}
	
	sPos = aRes->nMaxCount - aRes->nTotalCount;
	
    //lisz add 判断0
    float fTemp;
	for (i = sPos; i<aRes->nMaxCount; i++) {
        fTemp = QuoteLvKIndex_calcAVEDEV(TYP, i, aKIndex->CCI_N, TYPAVG[i]);
        if(fTemp!=0)
        {
            aKIndex->CCI.CCI[i] = (TYP[i] - TYPAVG[i])/(0.015 * fTemp);
        }
        else
        {
            aKIndex->CCI.CCI[i] = 0.0;
        }
	}
}

void QuoteLvKIndex_calcOBV(QUOTELV_K_INDEX* aKIndex, QUOTELV_K_RES* aRes)
{
  /*VA:=IF(CLOSE>REF(CLOSE,1),VOL,-VOL);
  OBV:SUM(IF(CLOSE=REF(CLOSE,1),0,VA),0);
  MAOBV:MA(OBV,M);*/
  
//  VA赋值:如果收盘价>1日前的收盘价,返回成交量(手),否则返回-成交量(手)
//  输出OBV:如果收盘价=1日前的收盘价,返回0,否则返回VA的历史累和
//  输出MAOBV:OBV的M日简单移动平均
  
  int sPos = aRes->nMaxCount - aRes->nTotalCount - aKIndex->OBV_N;		//起始位置
  sPos = sPos > 0 ? sPos : 0;
  
  int i = 0;
  double VA[KLIN_MAX_NUM];
//  double VAAVG[KLIN_MAX_NUM];
  
  for (i = sPos; i<aRes->nMaxCount; i++) {
    VA[i] = aRes->kData[i].fCur > aRes->kData[i-1<0?0:i-1].fCur
            ? aRes->kData[i].lVol:(-aRes->kData[i].lVol);
  }
  
//  VAAVG[0] = 0;
//  //VA的历史累和
//  for (i = sPos; i<aRes->nMaxCount; i++) {
//    VAAVG[i]=VAAVG[i-1<0?0:i-1]+VA[i];
//  }
  
  for (i = sPos; i<aRes->nMaxCount; i++) {
    int count = aRes->kData[i].fCur - aRes->kData[i-1<0?0:i-1].fCur;
    aKIndex->OBV.OBV[i] = (count == 0) ? 0:QuoteLvKIndex_calcSUM(VA,i,sPos);
  }
  
  for (i = sPos; i<aRes->nMaxCount; i++) {
    aKIndex->OBV.MAOBV[i] = QuoteLvKIndex_calcArrayMA(aKIndex->OBV.OBV,i,aKIndex->OBV_N);
  }
}

void QuoteLvKIndex_calcBIAS(QUOTELV_K_INDEX* aKIndex, QUOTELV_K_RES* aRes)
{
	/*
	 BIAS1 : (CLOSE-MA(CLOSE,L1))/MA(CLOSE,L1)*100;
	 BIAS2 : (CLOSE-MA(CLOSE,L2))/MA(CLOSE,L2)*100;
	 BIAS3 : (CLOSE-MA(CLOSE,L3))/MA(CLOSE,L3)*100;
	 */
	int MaxCycle = aKIndex->BIAS_L1 > aKIndex->BIAS_L2 ? aKIndex->BIAS_L1 : aKIndex->BIAS_L2;
	MaxCycle = MaxCycle > aKIndex->BIAS_L3 ? MaxCycle : aKIndex->BIAS_L3;
	int sPos = aRes->nMaxCount - aRes->nTotalCount - MaxCycle;		//起始位置
	sPos = sPos > 0 ? sPos : 0;
	
	int i = 0;
	double AVGL1[KLIN_MAX_NUM];
	double AVGL2[KLIN_MAX_NUM];
	double AVGL3[KLIN_MAX_NUM];
	
	for (i = sPos; i<aRes->nMaxCount; i++) {
		AVGL1[i] = QuoteLvKIndex_calcMA(aRes->kData, i, aKIndex->BIAS_L1);
		AVGL2[i] = QuoteLvKIndex_calcMA(aRes->kData, i, aKIndex->BIAS_L2);
		AVGL3[i] = QuoteLvKIndex_calcMA(aRes->kData, i, aKIndex->BIAS_L3);
	}
	
	sPos = aRes->nMaxCount - aRes->nTotalCount;
	
    //lisz add 判断0时的情况
	for (i = sPos; i<aRes->nMaxCount; i++) {
        if(AVGL1[i]!=0)
        {
            aKIndex->BIAS.BIAS1[i] = ((aRes->kData[i].fCur - AVGL1[i])/AVGL1[i])*100;
        }
        else
        {
            aKIndex->BIAS.BIAS1[i] = 0;
        }
        if(AVGL2[i]!=0)
        {
            aKIndex->BIAS.BIAS2[i] = ((aRes->kData[i].fCur - AVGL2[i])/AVGL2[i])*100;
        }
        else
        {
            aKIndex->BIAS.BIAS2[i] = 0;
        }
		
        if(AVGL3[i]!=0)
        {
            aKIndex->BIAS.BIAS3[i] = ((aRes->kData[i].fCur - AVGL3[i])/AVGL3[i])*100;
        }
        else
        {
            aKIndex->BIAS.BIAS3[i] = 0;
        }
	}
}

void QuoteLvKIndex_calcARBR(QUOTELV_K_INDEX* aKIndex, QUOTELV_K_RES* aRes)
{
	/*
	 AR : SUM(HIGH-OPEN,N)/SUM(OPEN-LOW,N)*100;
	 BR : SUM(MAX(0,HIGH-REF(CLOSE,1)),N)/SUM(MAX(0,REF(CLOSE,1)-LOW),N)*100;
	 */
	int sPos = aRes->nMaxCount - aRes->nTotalCount - aKIndex->ARBR_N;		//起始位置
	sPos = sPos > 0 ? sPos : 0;
	
	int i = 0;
	double SUM1[KLIN_MAX_NUM];
	double SUM2[KLIN_MAX_NUM];
	double MAX1[KLIN_MAX_NUM];
	double MAX2[KLIN_MAX_NUM];
	double MAX3[KLIN_MAX_NUM];
	double MAX4[KLIN_MAX_NUM];
	double SUM3[KLIN_MAX_NUM];
	double SUM4[KLIN_MAX_NUM];
	
	for (i = sPos; i<aRes->nMaxCount; i++) {
		MAX1[i] = aRes->kData[i].fHigh - aRes->kData[i].fOpen;
		MAX2[i] = aRes->kData[i].fOpen - aRes->kData[i].fLow;
		MAX3[i] = max(0,aRes->kData[i].fHigh - aRes->kData[i-1<0?0:i-1].fCur);
		MAX4[i] = max(0,aRes->kData[i-1<0?0:i-1].fCur - aRes->kData[i].fLow);
	}
	
	for (i = sPos; i<aRes->nMaxCount; i++) {
		SUM1[i] = QuoteLvKIndex_calcSUM(MAX1, i, aKIndex->ARBR_N);
		SUM2[i] = QuoteLvKIndex_calcSUM(MAX2, i, aKIndex->ARBR_N);
		SUM3[i] = QuoteLvKIndex_calcSUM(MAX3, i, aKIndex->ARBR_N);
		SUM4[i] = QuoteLvKIndex_calcSUM(MAX4, i, aKIndex->ARBR_N);

	}
	
	sPos = aRes->nMaxCount - aRes->nTotalCount;
	
	for (i = sPos; i<aRes->nMaxCount; i++) {
		//add by jason 20111103 修改算法和android相同
        if(SUM1[i]!=0 && SUM2[i]!=0)
        {
            aKIndex->ARBR.AR[i] = (SUM1[i]/SUM2[i])*100;
        }
		else
        {
            aKIndex->ARBR.AR[i] = 0;
        }
        if(SUM3[i]!=0 && SUM4[i]!=0)
        {
            aKIndex->ARBR.BR[i] = (SUM3[i]/SUM4[i])*100;
        }
        else
        {
            aKIndex->ARBR.BR[i] = 0;
        }
//		 aKIndex->ARBR.AR[i] = (SUM1[i]/SUM2[i])*100;
//		 aKIndex->ARBR.BR[i] = (SUM3[i]/SUM4[i])*100;
		
	}
}

void QuoteLvKIndex_calcPSY(QUOTELV_K_INDEX* aKIndex, QUOTELV_K_RES* aRes)
{
	/*
	COUNT(CLOSE>REF(CLOSE,1),N)/N*100;
	 */
	int sPos = aRes->nMaxCount - aRes->nTotalCount - aKIndex->PSY_N;		//起始位置
	sPos = sPos > 0 ? sPos : 0;
	
	int i = 0;
	double COUNT[KLIN_MAX_NUM];
	
	for (i = sPos; i<aRes->nMaxCount; i++) {
		COUNT[i] = QuoteLvKIndex_calcCOUNT(aRes->kData,i,aKIndex->PSY_N);
	}

	sPos = aRes->nMaxCount - aRes->nTotalCount;
	
	for (i = sPos; i<aRes->nMaxCount; i++) {
		aKIndex->PSY.PSY[i] = COUNT[i]/aKIndex->PSY_N*100;
	}
}

//lisz add 20110916 start
void QuoteLvKIndex_calcDDX(QUOTELV_K_INDEX* aKIndex, QUOTELV_K_RES* aRes)
{
	memset(aKIndex->DDX.ddx,0,sizeof(aKIndex->DDX.ddx));
	memset(aKIndex->DDX.ddx1,0,sizeof(aKIndex->DDX.ddx1));
	memset(aKIndex->DDX.ddx2,0,sizeof(aKIndex->DDX.ddx2));
	memset(aKIndex->DDX.ddx3,0,sizeof(aKIndex->DDX.ddx3));
	if (aRes->nEffectiveCount<=0) {
		return;
	}
  //printf("\nlFloatShare=%lld",aRes->lFloatShare[0]);
  for (int i = 0; i < aRes->nMaxCount; i++) {
    aKIndex->DDX.ddx[i] = (aRes->leve2KData[i].lMainBidVolume - aRes->leve2KData[i].lMainAskVolume)*100.0/aRes->lFloatShare[0];
    //printf("\nddx[%d]=%lf\tmbv:%lld\tmav:%lld",i, aKIndex->DDX.ddx[i],aRes->leve2KData[i].lMainBidVolume,aRes->leve2KData[i].lMainAskVolume);
  }
  int effectiveCycle = min(K_INDEX_L2XX_N1, aRes->nEffectiveCount);
  
  int i = 1;
  aKIndex->DDX.ddx1[0] = aKIndex->DDX.ddx[0];
	for(; i<aRes->nMaxCount; i++)
	{
		aKIndex->DDX.ddx1[i] = QuoteLvKIndex_calcEMA(aKIndex->DDX.ddx1[i-1],aKIndex->DDX.ddx[i],effectiveCycle);
	}
  
  for (i=0; i <aRes->nMaxCount; i++) {
    aKIndex->DDX.ddx1[i] = aKIndex->DDX.ddx1[i]*effectiveCycle;
  }
  
  for (i = 0; i < aRes->nMaxCount; i++) {
    aKIndex->DDX.ddx2[i] = QuoteLvKIndex_calcArrayMA(aKIndex->DDX.ddx1, i, K_INDEX_L2XX_N2);
    aKIndex->DDX.ddx3[i] = QuoteLvKIndex_calcArrayMA(aKIndex->DDX.ddx1, i, K_INDEX_L2XX_N3);
  }
}

void QuoteLvKIndex_calcDDY(QUOTELV_K_INDEX* aKIndex, QUOTELV_K_RES* aRes)
{
  //DDY：(卖出单数-买入单数)/流通盘*EMA((2*成交量-大买单量-大卖单量)/( 卖出单数+买入单数)，60)*100
  //先计算(2*成交量-大买单量-大卖单量)/( 卖出单数+买入单数)
  double tempF[KLIN_MAX_NUM];
  double tempF1[KLIN_MAX_NUM];
  
  int effectiveCycle = min(K_INDEX_L2XX_N1, aRes->nEffectiveCount);
  int i = 0;
	
	memset(aKIndex->DDX.ddx,0,sizeof(aKIndex->DDX.ddx));
	memset(aKIndex->DDX.ddx1,0,sizeof(aKIndex->DDX.ddx1));
	memset(aKIndex->DDX.ddx2,0,sizeof(aKIndex->DDX.ddx2));
	memset(aKIndex->DDX.ddx3,0,sizeof(aKIndex->DDX.ddx3));
	
	if (aRes->nEffectiveCount<=0) {
		return;
	}
	
  for (; i < aRes->nMaxCount; i++) {
    //long64 askVol = aRes->leve2KData[i].lMainAskVolume + aRes->leve2KData[i].lRetailAskVolume;
    long64 bidVol = aRes->leve2KData[i].lMainBidVolume + aRes->leve2KData[i].lRetailBidVolume;
    long64 tradeCount = aRes->leve2KData[i].lMainAskTradeCount + 
      aRes->leve2KData[i].lMainBidTradeCount + 
      aRes->leve2KData[i].lRetailBidTradeCount + 
      aRes->leve2KData[i].lRetailAskTradeCount;
    if(tradeCount==0){
      tempF[i] = 0;
    }else
    {
      tempF[i] = (((bidVol<<1)-aRes->leve2KData[i].lMainAskVolume-aRes->leve2KData[i].lMainBidVolume+0.0)/tradeCount);
    }
  }
  
  i = 1;
  tempF1[0] = tempF[0];
	for(; i<aRes->nMaxCount; i++)
	{
		tempF1[i] = QuoteLvKIndex_calcEMA(tempF1[i-1],tempF[i],effectiveCycle);
	}
  
  for (i = 0; i < aRes->nMaxCount; i++) {
    
    long64 tradeCount = aRes->leve2KData[i].lMainAskTradeCount+aRes->leve2KData[i].lRetailAskTradeCount-aRes->leve2KData[i].lMainBidTradeCount-aRes->leve2KData[i].lRetailBidTradeCount;
    aKIndex->DDY.ddy[i] = tradeCount*100.0/aRes->lFloatShare[0]*tempF1[i];
  }
  
  i = 1;
  aKIndex->DDY.ddy1[0] = aKIndex->DDY.ddy[0];
	for(; i<aRes->nMaxCount; i++)
	{
		aKIndex->DDY.ddy1[i] = QuoteLvKIndex_calcEMA(aKIndex->DDY.ddy1[i-1],aKIndex->DDY.ddy[i],effectiveCycle);
	}
  
  for (i=0; i <aRes->nMaxCount; i++) {
    aKIndex->DDY.ddy1[i] = aKIndex->DDY.ddy1[i]*effectiveCycle;
  }
  
  for (i = 0; i < aRes->nMaxCount; i++) {
    aKIndex->DDY.ddy2[i] = QuoteLvKIndex_calcArrayMA(aKIndex->DDY.ddy1, i, K_INDEX_L2XX_N2);
    aKIndex->DDY.ddy3[i] = QuoteLvKIndex_calcArrayMA(aKIndex->DDY.ddy1, i, K_INDEX_L2XX_N3);
  }
}

void QuoteLvKIndex_calcDDZ(QUOTELV_K_INDEX* aKIndex, QUOTELV_K_RES* aRes)
{
	memset(aKIndex->DDX.ddx,0,sizeof(aKIndex->DDX.ddx));
	memset(aKIndex->DDX.ddx1,0,sizeof(aKIndex->DDX.ddx1));
	memset(aKIndex->DDX.ddx2,0,sizeof(aKIndex->DDX.ddx2));
	memset(aKIndex->DDX.ddx3,0,sizeof(aKIndex->DDX.ddx3));
	
	if (aRes->nEffectiveCount<=0) {
		return;
	}
	
  for (int i = 0; i < aRes->nMaxCount; i++) {
    
    long64 askCount = aRes->leve2KData[i].lMainAskTradeCount + aRes->leve2KData[i].lRetailAskTradeCount;
    long64 bidCount = aRes->leve2KData[i].lMainBidTradeCount + aRes->leve2KData[i].lRetailBidTradeCount;
    long64 bidVol = aRes->leve2KData[i].lMainBidVolume + aRes->leve2KData[i].lRetailBidVolume;
    long64 totalCount = askCount + bidCount;
    double tempBid = 0;//大单买入量/买入单数
    double tempAsk = 0;//大单卖出量/卖出单数
    if(bidCount != 0){
      tempBid = (aRes->leve2KData[i].lMainBidVolume+0.0)/bidCount;
    }
    if(askCount!=0){
      tempAsk = (aRes->leve2KData[i].lMainAskVolume+0.0)/askCount;
    }
   aKIndex->DDZ.ddz1[i] = (tempBid-tempAsk)/100;
    
    //DDZ
    if(totalCount==0||bidVol==0||aKIndex->DDZ.ddz1[i]==0){
      aKIndex->DDZ.ddz[i] = 0;
    }else{
      aKIndex->DDZ.ddz[i]= (aKIndex->DDZ.ddz1[i]*100)/((2*bidVol+0.0)/totalCount);
    }
  }
}

void QuoteLvKIndex_calcBBDRetai(QUOTELV_K_INDEX* aKIndex, QUOTELV_K_RES* aRes)
{
  if (aRes->nMaxCount<=0) {
    return;
  }
	memset(aKIndex->BBDRetai.shareholdingRatio,0,sizeof(aKIndex->BBDRetai.shareholdingRatio));
	memset(aKIndex->BBDRetai.currentFluctuation,0,sizeof(aKIndex->BBDRetai.currentFluctuation));

	if (aRes->nEffectiveCount<=0) {
		return;
	}
	
  aKIndex->BBDRetai.currentFluctuation[0] = (aRes->leve2KData[0].lRetailBidVolume - aRes->leve2KData[0].lRetailAskVolume)*100.0/aRes->lFloatShare[0];
  aKIndex->BBDRetai.shareholdingRatio[0] = aKIndex->BBDRetai.currentFluctuation[0];
  for (int i = 1; i < aRes->nMaxCount; i++) {
    aKIndex->BBDRetai.currentFluctuation[i] = (aRes->leve2KData[i].lRetailBidVolume - aRes->leve2KData[i].lRetailAskVolume)*100.0/aRes->lFloatShare[0];
    aKIndex->BBDRetai.shareholdingRatio[i] = aKIndex->BBDRetai.shareholdingRatio[i-1] + aKIndex->BBDRetai.currentFluctuation[i];
  }
}

void QuoteLvKIndex_calcBBDMain(QUOTELV_K_INDEX* aKIndex, QUOTELV_K_RES* aRes)
{
  if (aRes->nMaxCount<=0) {
    return;
  }
	memset(aKIndex->BBDMain.shareholdingRatio,0,sizeof(aKIndex->BBDMain.shareholdingRatio));
	memset(aKIndex->BBDMain.currentFluctuation,0,sizeof(aKIndex->BBDMain.currentFluctuation));
	
	if (aRes->nEffectiveCount<=0) {
		return;
	}
	
  aKIndex->BBDMain.currentFluctuation[0] = (aRes->leve2KData[0].lMainBidVolume - aRes->leve2KData[0].lMainAskVolume)*100.0/aRes->lFloatShare[0];
  aKIndex->BBDMain.shareholdingRatio[0] = aKIndex->BBDMain.currentFluctuation[0];
  for (int i = 1; i < aRes->nMaxCount; i++) {
    aKIndex->BBDMain.currentFluctuation[i] = (aRes->leve2KData[i].lMainBidVolume - aRes->leve2KData[i].lMainAskVolume)*100.0/aRes->lFloatShare[0];
    aKIndex->BBDMain.shareholdingRatio[i] = aKIndex->BBDMain.shareholdingRatio[i-1] + aKIndex->BBDMain.currentFluctuation[i];
  }
}

void QuoteLvKIndex_calcBBD(QUOTELV_K_INDEX* aKIndex, QUOTELV_K_RES* aRes)
{
	memset(aKIndex->BBD.bbd,0,sizeof(aKIndex->BBD.bbd));
	memset(aKIndex->BBD.sum1,0,sizeof(aKIndex->BBD.sum1));
	memset(aKIndex->BBD.sum2,0,sizeof(aKIndex->BBD.sum2));
	memset(aKIndex->BBD.sum3,0,sizeof(aKIndex->BBD.sum3));
	
	if (aRes->nEffectiveCount<=0) {
		return;
	}
	
  for (int i = 0; i < aRes->nMaxCount; i++) {
    aKIndex->BBD.bbd[i] = aRes->leve2KData[i].lMainBidAmount - aRes->leve2KData[i].lMainAskAmount;
    
    aKIndex->BBD.sum1[i] = QuoteLvKIndex_calcLong64SUM(aKIndex->BBD.bbd, i, K_INDEX_L2BBD_N1);
    aKIndex->BBD.sum2[i] = QuoteLvKIndex_calcLong64SUM(aKIndex->BBD.bbd, i, K_INDEX_L2BBD_N2);
    aKIndex->BBD.sum3[i] = QuoteLvKIndex_calcLong64SUM(aKIndex->BBD.bbd, i, K_INDEX_L2BBD_N3);
  }
}
#define CALC_AVG_VOL_SUM(a,b) b>0 ? (a+0.0)/b:0;
void QuoteLvKIndex_calcAvgVolSum(QUOTELV_K_INDEX* aKIndex, QUOTELV_K_RES* aRes)
{
	memset(aKIndex->AVG_VOL_SUM.avgVol,0,sizeof(aKIndex->AVG_VOL_SUM.avgVol));
	memset(aKIndex->AVG_VOL_SUM.avgSum,0,sizeof(aKIndex->AVG_VOL_SUM.avgSum));
	
	if (aRes->nTrancEffectiveCount<=0) {
		return;
	}
	
  for (int i = 0; i < aRes->nMaxCount; i++) {
    aKIndex->AVG_VOL_SUM.avgVol[i] = CALC_AVG_VOL_SUM(aRes->kData[i].lVol,
                                                      aRes->leve2TrancKData[i].lTotalTradeCount);
    
    aKIndex->AVG_VOL_SUM.avgSum[i] = CALC_AVG_VOL_SUM(aRes->kData[i].lSum,
                                                      aRes->leve2TrancKData[i].lTotalTradeCount);
  }
}


//lisz add 20110916 end

double QuoteLvKIndex_RSV(QUOTELV_K_INDEX* aKIndex, QUOTELV_K_POINT* aPoint, int aKCount, int aStartPos)
{
	double RSVt = 0;
	double low = QuoteLvKIndex_LLV(aPoint,aKCount,aStartPos,aKIndex->KDJ_N);				//计算周期内的最小值
	double high = QuoteLvKIndex_HHV(aPoint,aKCount, aStartPos,aKIndex->KDJ_N);				//计算周期内的最大值
	
	if (low == 0 || high == 0 || low == high) {
		RSVt = 50;
	}
	else {
		RSVt = (( aPoint[aStartPos].fCur - low)/(high - low))*100;
	}

	return RSVt;
}

double QuoteLvKIndex_LLV(QUOTELV_K_POINT* aPoint,int aKCount, int aStartPos, int aCycle)
{
	int i = aStartPos - aCycle + 1  > 0 ? aStartPos - aCycle + 1 : 0;					////判断是否满足周期，如果不满足，有多少计算多少
	double low = aPoint[aStartPos].fLow;

	for(; i<aStartPos; i++)
	{
		low = low < aPoint[i].fLow ? low : aPoint[i].fLow;
	}
	

	return low;
}

double QuoteLvKIndex_HHV(QUOTELV_K_POINT* aPoint,int aKCount, int aStartPos, int aCycle)
{
	int i = aStartPos - aCycle + 1 > 0 ? aStartPos - aCycle + 1 : 0;
	double high = aPoint[aStartPos].fHigh;

	for(; i<aStartPos; i++)
	{
		high = high > aPoint[i].fHigh ? high : aPoint[i].fHigh;
	}

	return high;
}

double QuoteLvKIndex_calcSMA(double aPreEMA, double x, int aCycle, double aM)
{
	double y=(aM/aCycle)*x+(aCycle-aM)*aPreEMA/(aCycle);
	return y;
}

double QuoteLvKIndex_calcEMA(double aPreEMA, double x, int aCycle)
{
	double y=(2*x+(aCycle-1)*aPreEMA)/(aCycle+1);
	return y;
}

double QuoteLvKIndex_calcMA(QUOTELV_K_POINT* aPoint, int aStartPos, int aCycle)
{
	int i = aStartPos - aCycle + 1;
	double ma = 0;
	double nMax = 0;

	if (i < 0)
	{
		return ma;
	}

	for (; i<=aStartPos; i++)
	{
		nMax += aPoint[i].fCur;
	}

	ma = nMax/aCycle;
	
	return ma;
}

double QuoteLvKIndex_calcVMA(QUOTELV_K_POINT* aPoint, int aStartPos, int aCycle)
{
	int i = aStartPos - aCycle + 1;
	double ma = 0;
	double nMax = 0;

	if (i < 0)
	{
		return ma;
	}

	for (; i<=aStartPos; i++)
	{
		nMax += aPoint[i].lVol;
	}

	ma = nMax/aCycle;

	return ma;
}

double QuoteLvKIndex_calcArrayMA(double aPoint[], int aStartPos, int aCycle)
{
	int i = aStartPos - aCycle + 1;
	double ma = 0;
	double nMax = 0;

	if (i < 0)
	{
		return ma;
	}

	for (; i<=aStartPos; i++)
	{
		nMax += aPoint[i];
	}

	ma = nMax/aCycle;

	return ma;
}

double QuoteLvKIndex_calcSTD(QUOTELV_K_POINT* aPoint, int aStartPos, int aCycle, double aAvg)
{
	int i = aStartPos - aCycle + 1;
	double std = 0;
	double nMax = 0;

	if (i < 0 || aAvg == 0)
	{
		return std;
	}

	for (; i<=aStartPos; i++)
	{
		nMax += pow(aPoint[i].fCur-aAvg,2);
	}

	std = nMax/(aCycle-1);

	return sqrt(std);
}
		 
double QuoteLvKIndex_calcArraySTD(double aPoint[], int aStartPos, int aCycle, double aAvg)
{
	int i = aStartPos - aCycle + 1;
	double std = 0;
	double nMax = 0;

	if (i < 0 || aAvg == 0)
	{
		return std;
	}

	for (; i<=aStartPos; i++)
	{
		nMax += pow(aPoint[i]-aAvg,2);
	}

	std = nMax/(aCycle-1);

	return sqrt(std);
}

double QuoteLvKIndex_calcAVEDEV(double aPoint[], int aStartPos, int aCycle, double aAvg)
{
	
	int i = aStartPos - aCycle + 1;
	double std = 0;
	double nMax = 0;
	
	if (i < 0 || aAvg == 0)
	{
		return aAvg;
	}
	
	for (; i<=aStartPos; i++)
	{
		nMax += fabs(aPoint[i]-aAvg);
	}
	if(nMax!=0 && aCycle!=0)
    {
        std = nMax/aCycle;
    }
    else
    {
        std = 0.0;
    }
	
	return std;
	
}

double QuoteLvKIndex_calcEXPMEMA(double aPoint[], int aCount, double x, int aCycle)
{
	double aPreEMA = 0;
	int i = 0;

	for(; i<aCount; i++)
	{
		aPreEMA += aPoint[i];
	}

	aPreEMA = aPreEMA/aCount;

	double y = (aCycle*x + (aCycle - 1) * aPoint[aCount-1])/(aCycle + 1);
	return y;
}

double QuoteLvKIndex_calcSUM(double aPoint[], int aStartPos, int aCycle)
{
	int i = aStartPos - aCycle + 1;
	i  = i < 0 ? 0 : i;
	
	double nMax = 0;
	
	for (; i<=aStartPos; i++)
	{
		nMax += aPoint[i];
	}
	
	return nMax;
}

long64 QuoteLvKIndex_calcLong64SUM(long64 aPoint[], int aStartPos, int aCycle)
{
  int i = aStartPos - aCycle + 1;
	i  = i < 0 ? 0 : i;
	
	long64 nMax = 0;
	
	for (; i<=aStartPos; i++)
	{
		nMax += aPoint[i];
	}
	
	return nMax;
}

double QuoteLvKIndex_calcCOUNT(QUOTELV_K_POINT* aPoint, int aStartPos, int aCycle)
{
	int i = aStartPos - aCycle + 1;
	i  = i < 1 ? 1 : i;
	
	double nCount = 0;
	
	for (; i<=aStartPos; i++)
	{
		if (aPoint[i].fCur > aPoint[i-1].fCur) {
			nCount++;
		}
	}
	
	return nCount;
}
